/********************************************
 * 功能说明: OrganizationEmployeeRole实体对应的业务逻辑Service实现类
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.service;

import com.spring.arch.uaa.enums.Constant;
import com.spring.arch.uaa.model.OrganizationEmployeeRole;
import com.spring.arch.uaa.model.TenantEmployee;
import com.spring.arch.uaa.model.User;
import com.spring.arch.uaa.model.OrganizationEmployeeRole;
import com.spring.arch.uaa.model.TenantEmployee;
import com.spring.arch.uaa.model.User;
import com.spring.arch.uaa.repository.OrganizationEmployeeRepository;
import com.spring.arch.uaa.repository.OrganizationEmployeeRoleRepository;
import com.spring.arch.uaa.repository.UserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

import static com.spring.arch.uaa.enums.Constant.GLOBAL_DEFAULT_PASSWORD;

/**
 * OrganizationEmployeeRole实体对应的业务逻辑Service实现类
 *
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrganizationEmployeeRoleService {

    @Autowired
    private OrganizationEmployeeRoleRepository organizationEmployeeRoleRepository;

    @Autowired
    private OrganizationEmployeeRepository organizationEmployeeRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public void save(OrganizationEmployeeRole model) {
        this.organizationEmployeeRoleRepository.save(model);
    }

    public void saveAll(List<OrganizationEmployeeRole> model) {

        this.organizationEmployeeRoleRepository.deleteAllByOrganizationEmployee(model.get(0).getOrganizationEmployee());
        this.organizationEmployeeRoleRepository.saveAll(model);

        if(model.get(0).getOrganizationEmployee().getUser() == null) {

            TenantEmployee tenantEmployee = model.get(0).getOrganizationEmployee().getEmployee();

            int salt = (int) (Math.random() * 10000000);

            String pwd = passwordEncoder.encode(StringUtils.joinWith("$", Constant.GLOBAL_DEFAULT_PASSWORD, salt));

            User user = new User(tenantEmployee.getMobile(), null, tenantEmployee.getName(), pwd, String.valueOf(salt)
                    , tenantEmployee.getEmail(), tenantEmployee.getGender(), null);

            user = userRepository.save(user);

            model.get(0).getOrganizationEmployee().setUser(user);
            organizationEmployeeRepository.save(model.get(0).getOrganizationEmployee());
        }
    }

    public void deleteById(UUID id) {
        this.organizationEmployeeRoleRepository.deleteById(id);
    }

    public void deleteByOrganizationEmployee(UUID organizationEmployeeId) {
        this.organizationEmployeeRoleRepository.deleteAllByOrganizationEmployeeId(organizationEmployeeId);
    }
}
